<?php

namespace app\common\logic;
use app\common\basics\Logic;
use think\Db;


/**
 * 工作台统计
 * Class StatLogic
 * @package app\admin\logic\index
 */
class TechnLevelLogic extends Logic
{
//    处理技师等级,当前进度,各个等级关系
    public static function techn_level($techn_id,$jzl,$proxy_id){
        $level_list = Db::name('techn_level')
            ->where('is_delete_time',0)
            ->where('proxy_id',$proxy_id)
//            ->where('rank','>',$info['rank'])
            ->order('rank asc')
            ->select();

        if (empty($level_list)){
            $level_list = Db::name('techn_level')
                ->where('is_delete_time',0)
                ->where('proxy_id',0)
//                ->where('rank','>',$info['rank'])
                ->order('rank asc')
                ->select();
        }
        $info = Db::name('techn_last')->where('techn_id',$techn_id)->find();
        if (!empty($level_list)){
            foreach ($level_list as $key => $value){
                if ($value['low_result'] == 0){
                    $yeji = 100;
                }else{
                    $yeji = $info['total_result'] > $value['low_result'] ? 100 : ($info['total_result']/$value['low_result'])*100;
                }
                if ($value['online_duration'] == 0){
                    $shichang = 100;
                }else{
                    $shichang = $info['total_duration'] > $value['online_duration'] ? 100 : ($info['total_duration']/$value['online_duration'])*100;
                }
                if ($value['plus_clock_rate'] == 0){
                    $jzbl = 100;
                }else{
                    $jzbl = $jzl > $value['plus_clock_rate'] ? 100 : ($jzl/$value['plus_clock_rate'])*100;
                }
                if ($info['total_integral'] == 0){
                    $jzl = 100;
                }else{
                    $jzl = $info['total_integral'] > $value['integral'] ? 100 : ($info['total_integral']/$value['integral'])*100;
                }
                $all_bl = ($yeji+$shichang+$jzbl+$jzl)/4;
                $level_list[$key]['all_bl'] = (int)$all_bl;
                $level_list[$key]['level_img'] = '/static/'.$value['rank'].'x.png';
            }
        }
        return $level_list;
    }
//    加钟率计算
    public static function jz_b($techn_id,$min_month,$max_month){
        //开始计算加钟率 需要techn_id,day
        $order_ids = Db::name('order')
            ->where('techn_id',$techn_id)
            ->where('status',6)
            ->where('add_time','>=',$min_month)
            ->where('add_time','<',$max_month)
            ->column('id');

        $order_num = Db::name('order')
            ->where('techn_id',$techn_id)
            ->where('status',6)
            ->where('add_time','>=',$min_month)
            ->where('add_time','<',$max_month)
            ->sum('price');
        $jz_money = Db::name('order_upgrade')->where('order_id','in',$order_ids)->sum('price');
        $jz_money += Db::name('order_jz')->where('order_id','in',$order_ids)->where('zftype',1)->sum('price');


        if ($order_num > 0)
            $jzl = (int)($jz_money/($order_num)*100);
        else
            $jzl = 0;
        //结束计算加钟比例
        return $jzl;
    }

    public static function jz_v($techn_id,$min_month,$max_month){
        //开始计算加钟率 需要techn_id,day
        $order_num = Db::name('order')
            ->where('techn_id',$techn_id)
            ->where('status',6)
            ->where('add_time','>=',$min_month)
            ->where('add_time','<',$max_month)
            ->column('id');


        $jz_money = 0;
//        foreach ($order_num as $valuess){
            $jz_money = Db::name('order_jz')->where('order_id','in',$order_num)->where('zftype',1)->sum('price');
            $jz_money += Db::name('order_upgrade')->where('order_id','in',$order_num)->where('zftype',1)->sum('price');
//        }
        $order_num = Db::name('order')
            ->where('techn_id',$techn_id)
            ->where('status',6)
            ->where('add_time','>=',$min_month)
            ->where('add_time','<',$max_month)
            ->sum('price');

        if ($techn_id == 405){
//            dump($order_num);
        }


        if ($order_num > 0)
            $jzl = (int)($jz_money/($order_num)*100);
        else
            $jzl = 0;
        //结束计算加钟比例
        return $jzl;
    }

    //计算升级时加钟率
    public static function techn_add_level_jzl($day,$techn_id){
        if ($day == 1){
            $min = strtotime(date('Y-',time()).(date('m',time())-1).'-15');
            $max = strtotime(date('Y-',time()).(date('m',time())+1));
        }
        if ($day == 16){
            $min = strtotime(date('Y-m-1',time()));
            $max = $min + 1296000;
        }
        $min_month = $min;
        $max_month = $max;
        $order_ids = Db::name('order')
            ->where('techn_id',$techn_id)
            ->where('status',6)
            ->where('add_time','>=',$min_month)
            ->where('add_time','<',$max_month)
            ->column('id');

        $order_num = Db::name('order')
            ->where('techn_id',$techn_id)
            ->where('status',6)
            ->where('add_time','>=',$min_month)
            ->where('add_time','<',$max_month)
            ->sum('price');
        $jz_money = Db::name('order_upgrade')->where('order_id','in',$order_ids)->sum('price');
        $jz_money += Db::name('order_jz')->where('order_id','in',$order_ids)->where('zftype',1)->sum('price');


        if ($order_num > 0)
            $jzl = (int)($jz_money/($order_num)*100);
        else
            $jzl = 0;
        //结束计算加钟比例
        return $jzl;
    }

}